package com.zh.freechat.domain.message.repository;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zh.freechat.domain.message.entity.ChatGroupPO;
import com.zh.freechat.domain.message.entity.GroupMemberPO;
import com.zh.freechat.domain.message.repository.real.GroupMemberPoMapper;
import com.zh.freechat.domain.message.repository.real.GroupPoMapper;
import com.zh.freechat.domain.user.entity.GroupRelId;
import com.zh.webcommon.common.exception.NotFoundException;
import com.zh.webcommon.common.support.CopyFieldUtil;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ZH
 * @date 23:21 2021/5/15
 */
@AllArgsConstructor
@Repository
public class ChatGroupRepository {
    private final GroupPoMapper groupPoMapper;
    private final GroupMemberPoMapper groupMemberPoMapper;

    @Transactional
    public void save(ChatGroupPO groupRelPO) {
        groupPoMapper.insert(groupRelPO); //先save，才能有groupId
        groupRelPO.getUsers().forEach(e -> e.setGroupId(groupRelPO.getId()));
        groupMemberPoMapper.insert(groupRelPO.getUsers());
    }

    public void update(ChatGroupPO groupPO) {
        //先查再更新
        ChatGroupPO dbGroupPO = this.findByGroupId(groupPO.getId());
        CopyFieldUtil.copyPropertiesIgnoreNull(groupPO, dbGroupPO);
        groupPoMapper.updateById(dbGroupPO);
    }

    @NotNull
    public ChatGroupPO findByGroupId(Long groupId) {
        ChatGroupPO groupPO = groupPoMapper.selectById(groupId);
        if (groupPO == null) {
            throw new NotFoundException("找不到id=" + groupId + "的群聊组");
        }
        return groupPO;
        //        return wrapChatGroupRel(groupRelPO);

    }

    @Nullable
    public ChatGroupPO findByGroupNo(String groupNo) {
        ChatGroupPO groupRelPO = groupPoMapper.findByGroupNo(groupNo);
        if (groupRelPO == null) {
            throw new NotFoundException("找不到groupNo=" + groupNo + "的群聊组");
        }
        return groupRelPO;
//        return wrapChatGroupRel(groupRelPO);
    }

    public boolean existByGroupNo(String groupNo) {
        var query = new LambdaQueryWrapper<ChatGroupPO>()
                .eq(ChatGroupPO::getGroupNo, groupNo);
        return groupPoMapper.exists(query);
    }

    public void deleteByGroupNo(String groupNo) {
        var query = new LambdaQueryWrapper<ChatGroupPO>()
                .eq(ChatGroupPO::getGroupNo, groupNo);
        groupPoMapper.delete(query);
    }

    /**
     * 批量添加成员
     *
     * @param member
     * @param groupNo
     */
    public void addMember(GroupMemberPO member, String groupNo) {
        var groupId = groupPoMapper.getIdByNo(groupNo);
        member.setGroupId(groupId);
        groupMemberPoMapper.insert(member);
    }

    /**
     * 删除成员
     *
     * @param groupNo
     */
    public boolean removeMember(String userId, String groupNo) {
        var groupId = groupPoMapper.getIdByNo(groupNo);

        var query = new LambdaQueryWrapper<GroupMemberPO>()
                .eq(GroupMemberPO::getUserId, userId)
                .eq(GroupMemberPO::getGroupId, groupId);
        return groupMemberPoMapper.delete(query) > 0;
    }

    public List<ChatGroupPO> findGroupsByMember(String userId) {
        return groupPoMapper.findGroupsByMember(userId);
    }


    public List<GroupRelId> findRelIdAll() {
        return groupMemberPoMapper.findRelIdAll();
    }

}
